//
//  OtherViewController.m
//  cityStar
//
//  Created by joyban on 15-1-21.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "OtherViewController.h"

#import "SingleTableView.h"
#import "OtherCell.h"

#import "SVPullToRefresh.h"
#import "OtherPublishViewController.h"
#import "LoginCityStarViewViewController.h"
#import "BaseNavController.h"

NSString *const OtherCellID = @"otherCellID";


@interface OtherViewController ()<SingleTableViewDelegate>

@property (weak, nonatomic) IBOutlet UIButton *Alltype_btn;
@property (weak, nonatomic) IBOutlet UITableView *OtherTableView;

@property (weak, nonatomic) IBOutlet UIButton *Intelligence_btn;

@property (nonatomic, weak) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIImageView *typeImageView;
@property (strong, nonatomic) SingleTableView *typeSingleTableView;

@property (nonatomic, weak) IBOutlet UIImageView *noopsycheImageView;
@property (strong, nonatomic) SingleTableView *noopsycheSingleTableView;

@property (nonatomic, assign) int page;
@property (nonatomic, strong) NSMutableArray *arrayRequest;
@property (nonatomic, weak) IBOutlet UILabel *nothinglabel;

@end

@implementation OtherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"其他";
    [self addBackButton];
    [self setUpTableViews];
    __weak typeof(self)weakSelf = self;
    [self addRightButtonWithTitle:@"    发布" actionBlock:^{
        if (weakSelf.currentUser.nickname) {
        OtherPublishViewController *otherVC = [[OtherPublishViewController alloc]init];
        [weakSelf.navigationController pushViewController:otherVC animated:YES];
        }else{
            [weakSelf presentLoginViewController];
        }
    }];
    
    self.arrayRequest = [[NSMutableArray alloc]init];
    [self.OtherTableView addPullToRefreshWithActionHandler:^{
        self.page = 1;
        [weakSelf webRequest_GetOtherMore:NO];
    }];
    [self.OtherTableView addInfiniteScrollingWithActionHandler:^{
        [weakSelf webRequest_GetOtherMore:YES];
    }];
    [self.OtherTableView triggerPullToRefresh];
}

- (void)webRequest_GetOtherMore:(BOOL)isMore
{
    NSString *postUrl = [NSString stringWithFormat:@"%@/type/%d/page/%d/mine/%d",KInterface_PublishList,1,isMore?self.page:0,0];
    [HttpRequestManager getMethod:[postUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            DLog(@"%@",response);
            if (isMore) {
                [self.arrayRequest addObjectsFromArray:response[@"value"][0][@"content"]];
                self.page = self.page + 1;
            }else{
                self.arrayRequest = [[NSMutableArray alloc]initWithArray:response[@"value"][0][@"content"]];
            }
            [self.OtherTableView reloadData];
            if (self.arrayRequest.count == 0) {
                self.nothinglabel.hidden = NO;
            }else{
                self.nothinglabel.hidden = YES;
            }
        }
        if (isMore){
            [self.OtherTableView.infiniteScrollingView stopAnimating];
        }else{
            [self.OtherTableView.pullToRefreshView stopAnimating];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setUpTableViews
{
    [self.OtherTableView registerNib:[OtherCell nib] forCellReuseIdentifier:OtherCellID];
}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [OtherCell heightOfTableCell];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayRequest.count;
}

#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OtherCell *ListdetailCell = [tableView dequeueReusableCellWithIdentifier:OtherCellID];
    ListdetailCell.selectionStyle = UITableViewCellSelectionStyleGray;
    return ListdetailCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView reloadData];
}

#pragma mark - Actions
-(IBAction)allTypes_atction:(id)sender
{
    if(self.Alltype_btn.selected == NO)
    {
        self.Alltype_btn.selected = YES;
    }else{
        self.Alltype_btn.selected = NO;
    }
    self.Intelligence_btn.selected = NO;
    
    [self.typeSingleTableView showFromPosition:CGPointMake(0, 25) inViewController:self dataType:SingleTableViewDataTypeSortModel];
    self.typeSingleTableView.dataArray = @[@"全部分类",@"餐饮类",@"便利配套类",@"食品类",@"酒店类",@"休闲娱乐类",@"百货商品类"];
}

-(IBAction)Intelligence_atction:(id)sender
{
    if(self.Intelligence_btn.selected == NO)
    {
        self.Intelligence_btn.selected = YES;
    }else{
        self.Intelligence_btn.selected = NO;
    }
    self.Alltype_btn.selected = NO;
    
    [self.noopsycheSingleTableView showFromPosition:CGPointMake(0, 25) inViewController:self dataType:SingleTableViewDataTypeSortModel];
    self.noopsycheSingleTableView.dataArray =@[@"智能排序",@"价格从低到高",@"价格从高到低",@"评论排序"];
}

#pragma mark - singleTableViewDelegate
-(SingleTableView *)typeSingleTableView
{
    if (!_typeSingleTableView) {
        _typeSingleTableView = [[SingleTableView alloc] init];
        _typeSingleTableView.delegate = self;
        [self addChildViewController:_typeSingleTableView];
        [self.view addSubview:_typeSingleTableView.view];
        [self.view bringSubviewToFront:self.topView];
        _typeSingleTableView.arrawImageView = self.typeImageView;
    }
    return _typeSingleTableView;
}

-(void)singleTableView:(SingleTableView *)tableView didSelectedCellAtIndexPath:(NSIndexPath *)indexPath
{
    self.Alltype_btn.selected = NO;
    self.Intelligence_btn.selected = NO;
    if (tableView == self.typeSingleTableView) {
        NSString *celltest = @[@"全部分类",@"餐饮类",@"便利配套类",@"食品类",@"酒店类",@"休闲娱乐类",@"百货商品类"][indexPath.row];
        if (celltest.length > 4) {
            NSString *btnTitle = [NSString stringWithFormat:@"%@..",[celltest substringWithRange:NSMakeRange(0, 4)]];
            [self.Alltype_btn setTitle:btnTitle forState:UIControlStateNormal];
        }else{
            [self.Alltype_btn setTitle:celltest forState:UIControlStateNormal];
            
        }
    }else
    {
        NSString *celltest =@[@"智能排序",@"价格从低到高",@"价格从高到低",@"评论排序"][indexPath.row];
        if (celltest.length > 4) {
            NSString *btnTitle = [NSString stringWithFormat:@"%@..",[celltest substringWithRange:NSMakeRange(0, 4)]];
            [self.Intelligence_btn setTitle:btnTitle forState:UIControlStateNormal];
        }else{
            [self.Intelligence_btn setTitle:celltest forState:UIControlStateNormal];
            
        }
    }
}

-(void)cancleTabelView
{
    self.Alltype_btn.selected = NO;
    self.Intelligence_btn.selected = NO;
}

-(SingleTableView *)noopsycheSingleTableView
{
    if (!_noopsycheSingleTableView) {
        _noopsycheSingleTableView = [[SingleTableView alloc] init];
        _noopsycheSingleTableView.delegate = self;
        [self addChildViewController:_noopsycheSingleTableView];
        [self.view addSubview:_noopsycheSingleTableView.view];
        [self.view bringSubviewToFront:self.topView];
        _noopsycheSingleTableView.arrawImageView = self.noopsycheImageView;
    }
    return _noopsycheSingleTableView;
}

#pragma mark - IsJumpToLoginViewController
-(void)presentLoginViewController
{
    LoginCityStarViewViewController *loginCityStarVC = [[LoginCityStarViewViewController alloc]init];
    BaseNavController *baseloginNav = [[BaseNavController alloc] initWithRootViewController:loginCityStarVC];
    [self presentViewController:baseloginNav animated:YES completion:^{
    }];
}

@end
